package com.xscr.api.service.item;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.rop.annotation.HttpAction;
import com.rop.annotation.IgnoreSignType;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.response.Response;
import com.xscr.api.pojo.CategoryDTO;
import com.xscr.api.pojo.CommentDTO;
import com.xscr.api.pojo.FilterDTO;
import com.xscr.api.pojo.FilterValue;
import com.xscr.api.pojo.ImageDTO;
import com.xscr.api.pojo.ProductDTO;
import com.xscr.api.pojo.ProductDetailDTO;
import com.xscr.api.pojo.PromotionDTO;
import com.xscr.api.pojo.SecondCategoryDTO;
import com.xscr.api.pojo.SkuDTO;
import com.xscr.api.request.BaseRequest;
import com.xscr.api.request.CategoryRequest;
import com.xscr.api.request.CircleRequest;
import com.xscr.api.request.GetEvaluationRequest;
import com.xscr.api.request.QueryProductRequest;
import com.xscr.api.service.forum.ForumAction;
import com.xscr.api.utils.Constants;
import com.xscr.cms.api.CmsHomeService;
import com.xscr.cms.api.CmsTopicService;
import com.xscr.cms.dto.CmsHomeDto;
import com.xscr.cms.dto.CmsTopicDto;
import com.xscr.cms.po.CmsHome;
import com.xscr.cms.po.CmsIndexImage;
import com.xscr.forum.po.ForumCircle;
import com.xscr.forum.service.ForumCircleService;
import com.xscr.item.api.ItemBrandService;
import com.xscr.item.api.ItemCategoryService;
import com.xscr.item.api.ItemPropertyNameService;
import com.xscr.item.api.ItemService;
import com.xscr.item.dto.ItemDetailDto;
import com.xscr.item.dto.ItemDto;
import com.xscr.item.po.Item;
import com.xscr.item.po.ItemBrand;
import com.xscr.item.po.ItemCategory;
import com.xscr.item.po.ItemPicture;
import com.xscr.item.po.ItemPropertyName;
import com.xscr.item.po.ItemPropertyValue;
import com.xscr.item.po.ItemSku;
import com.xscr.item.po.SkuPropertyValueLink;
import com.xscr.mybatis.utility.PageBean;
import com.xscr.order.api.OrderEvaluationService;
import com.xscr.order.dto.EvaluationDto;
import com.xscr.promotion.api.RuleService;
import com.xscr.promotion.po.ProRule;
import com.xscr.user.service.ICloginInfoService;

@ServiceMethodBean(version = "1.0", ignoreSign = IgnoreSignType.YES)
public class ItemAction {
	private static final Logger logger=LoggerFactory.getLogger(ItemAction.class);
	@Autowired
	private ItemService itemService;

	@Autowired
	private ItemCategoryService itemCategoryService;

	@Resource
	ItemPropertyNameService itemPropertyNameService;

	@Resource
	ItemBrandService itemBrandService;

	@Autowired
	private OrderEvaluationService orderEvaluationService;

	@Resource
	private RuleService ruleService;
	
	@Resource
	private CmsHomeService cmsHomeService;
	
	@Resource
	private CmsTopicService cmsTopicService;

	/**
	 * 首页
	 * 
	 * @return
	 */
	/**
	 * @param baseRequest
	 * @return
	 */
	@ServiceMethod(method = "item.getHome", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getHome(BaseRequest baseRequest) {
		Response response = new Response();
		String msg = "";

		try {
			Map<String, Object> data = new HashMap<String, Object>(5);
			Map<String, CmsHomeDto> homeList = cmsHomeService.selectCmsHomeList(2, 1);
			
			if(homeList==null)
			{
				response.getResult().setResult("0");
				response.getResult().setMessage("首页数据为空");
				return response;
			}
			
			List<ImageDTO> bannerList = new ArrayList<ImageDTO>();
			
			if(homeList.containsKey("app_banner"))
			{
				CmsHomeDto cmsHomeDto=(CmsHomeDto)homeList.get("app_banner");
				List<CmsIndexImage> cmsIndexImageList=cmsHomeDto.getCmsIndexImageList();
				if(cmsIndexImageList!=null){
				for(CmsIndexImage cmsIndexImage:cmsIndexImageList){
				    ImageDTO imgBase = new ImageDTO();	
				    imgBase.setImgUrl(cmsIndexImage.getImageUrl());
				    imgBase.setType(cmsIndexImage.getTargetType());
				    imgBase.setJumpId(cmsIndexImage.getTargetUrl());
				    imgBase.setSort(cmsIndexImage.getSort());
				    bannerList.add(imgBase);
				}
				}
			}
			
			List<ImageDTO> guideList = new ArrayList<ImageDTO>();
			if(homeList.containsKey("app_guide"))
			{
				CmsHomeDto cmsHomeDto=(CmsHomeDto)homeList.get("app_guide");
				List<CmsIndexImage> cmsIndexImageList=cmsHomeDto.getCmsIndexImageList();
				if(cmsIndexImageList!=null){
				for(CmsIndexImage cmsIndexImage:cmsIndexImageList){
				    ImageDTO imgBase = new ImageDTO();	
				    imgBase.setName(cmsIndexImage.getItemName());
				    imgBase.setImgUrl(cmsIndexImage.getImageUrl());
				    imgBase.setType(cmsIndexImage.getTargetType());
				    imgBase.setJumpId(cmsIndexImage.getTargetUrl());
				    imgBase.setSort(cmsIndexImage.getSort());
				    guideList.add(imgBase);
				}
				}
			}
			
			List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();

			if(homeList.containsKey("app_forum"))
			{
				Map<String, Object> guideMap = new HashMap<String, Object>();
				CmsHomeDto cmsHomeDto=(CmsHomeDto)homeList.get("app_forum");
				List<ImageDTO> dataList = new ArrayList<ImageDTO>();
				CmsHome cmsHome=cmsHomeDto.getCmsHome();
				List<CmsIndexImage> cmsIndexImageList=cmsHomeDto.getCmsIndexImageList();
				if(cmsIndexImageList!=null){
				for(CmsIndexImage cmsIndexImage:cmsIndexImageList){
				    ImageDTO imgBase = new ImageDTO();	
				    imgBase.setImgUrl(cmsIndexImage.getImageUrl());
				    imgBase.setType(cmsIndexImage.getTargetType());
				    imgBase.setJumpId(cmsIndexImage.getTargetUrl());
				    imgBase.setSort(cmsIndexImage.getSort());
				    dataList.add(imgBase);
				}
				}
				if(cmsIndexImageList!=null){
				guideMap.put("title", cmsHome.getBrandName());
				guideMap.put("dataList", dataList);
				guideMap.put("moreType", 2);
				guideMap.put("more",cmsHome.getBrandContent());
				}
				groupList.add(guideMap);				
			}
			
			if(homeList.containsKey("app_floor1"))
			{
				Map<String, Object> guideMap = new HashMap<String, Object>();
				CmsHomeDto cmsHomeDto=(CmsHomeDto)homeList.get("app_floor1");
				List<ImageDTO> dataList = new ArrayList<ImageDTO>();
				CmsHome cmsHome=cmsHomeDto.getCmsHome();
				List<CmsIndexImage> cmsIndexImageList=cmsHomeDto.getCmsIndexImageList();
				if(cmsIndexImageList!=null){
				for(CmsIndexImage cmsIndexImage:cmsIndexImageList){
				    ImageDTO imgBase = new ImageDTO();	
				    imgBase.setImgUrl(cmsIndexImage.getImageUrl());
				    imgBase.setType(cmsIndexImage.getTargetType());
				    imgBase.setJumpId(cmsIndexImage.getTargetUrl());
				    imgBase.setSort(cmsIndexImage.getSort());
				    dataList.add(imgBase);
				}
				}
				if(cmsIndexImageList!=null){
				guideMap.put("title", cmsHome.getBrandName());
				guideMap.put("dataList", dataList);
				guideMap.put("moreType", 2);
				guideMap.put("more",cmsHome.getBrandContent());
				}
				groupList.add(guideMap);				
			}
			
			if(homeList.containsKey("app_floor2"))
			{
				Map<String, Object> guideMap = new HashMap<String, Object>();
				CmsHomeDto cmsHomeDto=(CmsHomeDto)homeList.get("app_floor2");
				List<ImageDTO> dataList = new ArrayList<ImageDTO>();
				CmsHome cmsHome=cmsHomeDto.getCmsHome();
				List<CmsIndexImage> cmsIndexImageList=cmsHomeDto.getCmsIndexImageList();
				if(cmsIndexImageList!=null){
				for(CmsIndexImage cmsIndexImage:cmsIndexImageList){
				    ImageDTO imgBase = new ImageDTO();	
				    imgBase.setImgUrl(cmsIndexImage.getImageUrl());
				    imgBase.setType(cmsIndexImage.getTargetType());
				    imgBase.setJumpId(cmsIndexImage.getTargetUrl());
				    imgBase.setSort(cmsIndexImage.getSort());
				    dataList.add(imgBase);
				}
				}
				if(cmsIndexImageList!=null){
				guideMap.put("title", cmsHome.getBrandName());
				guideMap.put("dataList", dataList);
				guideMap.put("moreType", 2);
				guideMap.put("more",cmsHome.getBrandContent());
				}
				groupList.add(guideMap);				
			}
			
			if(homeList.containsKey("app_floor3"))
			{
				Map<String, Object> guideMap = new HashMap<String, Object>();
				CmsHomeDto cmsHomeDto=(CmsHomeDto)homeList.get("app_floor3");
				List<ImageDTO> dataList = new ArrayList<ImageDTO>();
				CmsHome cmsHome=cmsHomeDto.getCmsHome();
				List<CmsIndexImage> cmsIndexImageList=cmsHomeDto.getCmsIndexImageList();
				if(cmsIndexImageList!=null){
				for(CmsIndexImage cmsIndexImage:cmsIndexImageList){
				    ImageDTO imgBase = new ImageDTO();	
				    imgBase.setImgUrl(cmsIndexImage.getImageUrl());
				    imgBase.setType(cmsIndexImage.getTargetType());
				    imgBase.setJumpId(cmsIndexImage.getTargetUrl());
				    imgBase.setSort(cmsIndexImage.getSort());
				    dataList.add(imgBase);
				}
				}
				if(cmsIndexImageList!=null){
				guideMap.put("title", cmsHome.getBrandName());
				guideMap.put("dataList", dataList);
				guideMap.put("moreType", 2);
				guideMap.put("more",cmsHome.getBrandContent());
				}
				groupList.add(guideMap);				
			}
			
			data.put("banner", bannerList);
			data.put("guide", guideList);
			data.put("group", groupList);
			response.setData(data);
		} catch (Exception e) {
			logger.error("getHome error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取首页错误");
			return response;
		}
		msg = "获取首页成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}
	
	/**
	 * 专题页
	 * @param baseRequest
	 * @return
	 */
	@ServiceMethod(method = "item.getSpecial", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getSpecial(BaseRequest baseRequest) {
		Response response = new Response();
		String msg = "";
		
		Long specialId=baseRequest.getId();
		if(specialId==null||StringUtils.isBlank(specialId.toString())){
			response.getResult().setResult("0");
			response.getResult().setMessage("参数不合法");
			return response;
		}
			
		try {
			CmsTopicDto cmsTopicDto = cmsTopicService.selectCmsTopicList(Integer.valueOf(specialId.intValue()), 2);
			if(cmsTopicDto==null)
			{
				response.getResult().setResult("0");
				response.getResult().setMessage("专题数据为空");
				return response;
			}
			response.setData(cmsTopicDto);
		} catch (Exception e) {
			logger.error("getHome error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取专题错误");
			return response;
		}
		msg = "获取专题成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}

	/**
	 * 类目列表
	 * 
	 * @param categoryRequest
	 * @return
	 */
	@ServiceMethod(method = "item.getCategoryList", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getCategoryList(CategoryRequest categoryRequest) {
		Response response = new Response();
		String msg = "";

		String categoryId = categoryRequest.getCategoryId();
		Integer page = categoryRequest.getPageCount();
		
		if(categoryId==null||StringUtils.isBlank(categoryId))
		{
			response.getResult().setResult("0");
			response.getResult().setMessage("参数不合法");
			return response;
		}
		
		try {

			List<ItemCategory> itemCategoryList = itemCategoryService
					.selectCategoryTreeByPid(Integer.parseInt(categoryId));
			List<CategoryDTO> categoryList = new ArrayList<CategoryDTO>();
			CategoryDTO category = null;
			for (ItemCategory itemCategory : itemCategoryList) {
				category = new CategoryDTO();
				category.setCategoryId(Long.parseLong(itemCategory
						.getCategoryId().toString()));
				category.setCategoryName(itemCategory.getCategoryName());
				List<ItemCategory> subItemCategoryList = itemCategoryService
						.selectCategoryNumsByPid(itemCategory.getCategoryId());
				StringBuffer str = new StringBuffer();
				for (ItemCategory subItemCategory : subItemCategoryList) {
					str.append(subItemCategory.getCategoryName() + "/");
				}
				if (str.length() > 0)
					str.deleteCharAt(str.length() - 1);

				category.setDescription(str.toString());
				category.setImgUrl(itemCategory.getIcon());
				category.setParentId(Long.parseLong(itemCategory.getParentId()
						.toString()));
				categoryList.add(category);
			}

			response.setData(categoryList);
		} catch (Exception e) {
			logger.error("getCategoryList error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取类目列表错误");
			return response;
		}

		msg = "获取类目列表成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}
	
	/**
	 * 新类目列表
	 * @param categoryRequest
	 * @return
	 */
	@ServiceMethod(method = "item.getNewCategoryList", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getNewCategoryList(CategoryRequest categoryRequest) {
		Response response = new Response();
		String msg = "";

		String categoryId = categoryRequest.getCategoryId();
		Integer page = categoryRequest.getPageCount();
		
		if(categoryId==null||StringUtils.isBlank(categoryId))
		{
			response.getResult().setResult("0");
			response.getResult().setMessage("参数不合法");
			return response;
		}
		
		try {

			List<ItemCategory> itemCategoryList = itemCategoryService
					.selectCategoryTreeByPid(Integer.parseInt(categoryId));
			List<CategoryDTO> categoryList = new ArrayList<CategoryDTO>();
			CategoryDTO category = null;
			for (ItemCategory itemCategory : itemCategoryList) {
				category = new CategoryDTO();
				category.setCategoryId(Long.parseLong(itemCategory
						.getCategoryId().toString()));
				category.setCategoryName(itemCategory.getCategoryName());
				List<ItemCategory> subItemCategoryList = itemCategoryService
						.selectCategoryNumsByPid(itemCategory.getCategoryId());
				List<SecondCategoryDTO> secondCategoryList=new ArrayList<SecondCategoryDTO>();
				for (ItemCategory subItemCategory : subItemCategoryList) {
					SecondCategoryDTO secondCategory=new SecondCategoryDTO();
					secondCategory.setCategoryId(Long.parseLong(subItemCategory
						.getCategoryId().toString()));
					secondCategory.setCategoryName(subItemCategory.getCategoryName());
					secondCategory.setDescription("");
					secondCategory.setImgUrl(subItemCategory.getIcon());
					secondCategory.setParentId(Long.parseLong(subItemCategory.getParentId().toString()));
					secondCategoryList.add(secondCategory);
				}
				category.setSecondCategoryList(secondCategoryList);
				category.setDescription("");
				category.setImgUrl(itemCategory.getIcon());
				category.setParentId(Long.parseLong(itemCategory.getParentId()
						.toString()));
				categoryList.add(category);
			}

			response.setData(categoryList);
		} catch (Exception e) {
			logger.error("getCategoryList error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取类目列表错误");
			return response;
		}

		msg = "获取类目列表成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}

	/**
	 * 商品列表(通过类目ID获取 )
	 * 
	 * @param queryProductRequest
	 * @return
	 */
	@ServiceMethod(method = "item.getProductList", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getProductList(QueryProductRequest queryProductRequest) {
		Response response = new Response();
		String msg = "";

		// List<String> filterIdList =
		// queryProductRequest.getFilterValueIdList();
		String filterIds = queryProductRequest.getFilterValueIds();
		String categoryId = queryProductRequest.getCategoryId();
		String keyword = queryProductRequest.getKeyword();
		Integer page = queryProductRequest.getPageCount();
		Integer sortBy = queryProductRequest.getSortBy();
		String sortVal=queryProductRequest.getSortVal();
		
		if(categoryId==null||StringUtils.isBlank(categoryId))
		{
			response.getResult().setResult("0");
			response.getResult().setMessage("参数不合法");
			return response;
		}

		try {
			List<ItemCategory> itemCategory = itemCategoryService
					.selectCategoryTreeByPid(0);

			PageBean<Map<String, Object>> pageBean = new PageBean<Map<String, Object>>();
			pageBean.setPageSize(20);
			pageBean.setPage(page);
			pageBean.setOrder(sortVal);
			if (sortBy == 1)
				pageBean.setSortFields("t1.create_time");
			else if (sortBy == 2)
				pageBean.setSortFields("minprice");
			else if (sortBy == 3)
				pageBean.setSortFields("t1.sale_amount");

			List<Integer> valIds = new ArrayList<Integer>();
			if (!StringUtils.isBlank(filterIds)) {
				String[] skuIdsArray = filterIds.split("-");
				for (String skuId : skuIdsArray) {
					// valIds.add(Integer.valueOf(valId));
					valIds.add(Integer.valueOf(skuId));
				}
			}

			pageBean = itemService.searchItemListPage(keyword.trim(), valIds,
					Integer.parseInt(categoryId), 0, 0, pageBean);

			List<Map<String, Object>> pdList = pageBean.getResult();

			List<ProductDTO> productList = new ArrayList<ProductDTO>();
			for (Map<String, Object> map : pdList) {
				if (!map.containsKey("itemId"))
					continue;
				ProductDTO product = new ProductDTO();
				product.setImgUrl(map.get("imageUrl").toString());
				product.setName(map.get("itemZhName").toString());
				product.setPid(Long.parseLong(map.get("itemId").toString()));
				product.setPrice(new BigDecimal(map.get("minprice").toString()));
				product.setSalesVolume(1000);
				productList.add(product);
			}

			List<FilterDTO> filterList = new ArrayList<FilterDTO>();
			List<FilterValue> filterValueList = new ArrayList<FilterValue>();

			List<ItemBrand> brands = itemBrandService
					.selectBrendByCategoryId(Integer.parseInt(categoryId));
			FilterDTO filter = new FilterDTO();
			filter.setFilterId(0L);
			filter.setFilterName("品牌");

			for (ItemBrand brand : brands) {

				FilterValue filterValue = new FilterValue();
				filterValue.setFilterValueId(Long.parseLong(brand.getBrandId()
						.toString()));
				filterValue.setFilterValueName(brand.getBrandName());
				filterValueList.add(filterValue);
			}
			filter.setFilterValueList(filterValueList);
			filterList.add(filter);

			filterValueList = new ArrayList<FilterValue>();
			Map<ItemPropertyName, List<ItemPropertyValue>> propertys = itemPropertyNameService
					.selectPropertysByCateId(Integer.parseInt(categoryId));
			Set<ItemPropertyName> keySet = propertys.keySet();
			for (ItemPropertyName key : keySet) {
				List<ItemPropertyValue> value = propertys.get(key);
				filter = new FilterDTO();
				filter.setFilterId(Long.parseLong(key.getPropertyId()
						.toString()));
				filter.setFilterName(key.getPropertyName());

				for (ItemPropertyValue itemValue : value) {

					FilterValue filterValue = new FilterValue();
					filterValue.setFilterValueId(Long.parseLong(itemValue
							.getPropertyId().toString()));
					filterValue
							.setFilterValueName(itemValue.getPropertyValue());
					filterValueList.add(filterValue);
				}
				filter.setFilterValueList(filterValueList);
				filterList.add(filter);
			}

			Map<String, Object> data = new HashMap<String, Object>(5);
			data.put("productList", productList);
			data.put("filterList", filterList);
			response.setData(data);
		} catch (Exception e) {
			logger.error("getProductList error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取商品列表错误");
			return response;
		}
		msg = "获取商品列表成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}

	/**
	 * 商品列表(通过关键字获取 )
	 * 
	 * @param queryProductRequest
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ServiceMethod(method = "item.getProductListByKeyword", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getProductListByKeyword(QueryProductRequest queryProductRequest){
		Response response = new Response();
		String msg = "";
		String keyword=queryProductRequest.getKeyword();
		String categoryId = queryProductRequest.getCategoryId();
		Integer page = queryProductRequest.getPageCount();
		Integer tagId=0;
		
/*		if(keyword==null||StringUtils.isBlank(keyword))
		{
			response.getResult().setResult("0");
			response.getResult().setMessage("参数不合法");
			return response;
		}*/
		
		try {
			
	        if(keyword!=null&&StringUtils.isNotBlank(keyword))
			    keyword = new String(keyword.getBytes("iso8859-1"),"utf-8");
	        else
	        	keyword="";
			
			PageBean<Map<String, Object>> pageBean = new PageBean<Map<String, Object>>();
			pageBean.setPageSize(20);
			pageBean.setPage(page);
            if(categoryId!=null && !StringUtils.isBlank(categoryId))
            {
            	tagId=Integer.parseInt(categoryId);
            }
			pageBean = itemService.searchItemListPage(keyword.trim(), null, tagId,
					0, 0, pageBean);

			List<ProductDTO> productList = new ArrayList<ProductDTO>();
			for (Map<String, Object> map : pageBean.getResult()) {
				if (!map.containsKey("itemId"))
					continue;
				ProductDTO product = new ProductDTO();
				product.setImgUrl(map.get("imageUrl").toString());
				product.setName(map.get("itemZhName").toString());
				product.setPid(Long.parseLong(map.get("itemId").toString()));
				product.setPrice(new BigDecimal(map.get("minprice").toString()));
				product.setSalesVolume(1000);
				productList.add(product);
			}

			Map<String, Object> data = new HashMap<String, Object>(5);
			data.put("productList", productList);

			response.setData(data);
		} catch (Exception e) {
			logger.error("getProductListByKeyword error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取商品列表错误");
			return response;
		}
		msg = "获取商品列表成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}

	public PageBean<EvaluationDto> getItemEvaluation(Integer itemId,
			Integer page) {
		PageBean<EvaluationDto> pageBean = new PageBean<EvaluationDto>();
		pageBean.setPageSize(Constants.PUBLIC_STATIC_NUM_10);
		if (null == page) {
			page = Constants.PUBLIC_STATIC_NUM_1;
		}
		pageBean.setPage(page);
		pageBean = orderEvaluationService.handleShowItemEvaluation(itemId,
				pageBean);
		return pageBean;
	}

	/**
	 * 商品详情
	 * 
	 * @param baseRequest
	 * @return
	 */
	@ServiceMethod(method = "item.getProductDetail", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getProductDetail(BaseRequest baseRequest) {
		Response response = new Response();
		String msg = "";

		Long itemId = baseRequest.getId();
		
		if(itemId==null||StringUtils.isBlank(itemId.toString()))
		{
			response.getResult().setResult("0");
			response.getResult().setMessage("参数不合法");
			return response;			
		}

		try {
			ItemDetailDto itemDetail = itemService.selectDetailItem(
					Integer.valueOf(itemId.toString()), 3);
			ProductDetailDTO productDetail = new ProductDetailDTO();
			productDetail.setBuyerCount(100);

			// --------------------評論
			List<CommentDTO> commentList = new ArrayList<CommentDTO>();

			PageBean<EvaluationDto> pageBean = getItemEvaluation(
					Integer.valueOf(itemId.toString()), 1);
			List<EvaluationDto> evaluationList = pageBean.getResult();
			if (evaluationList != null) {
				for (EvaluationDto evaluation : evaluationList) {
					CommentDTO comment = new CommentDTO();
					comment.setCreaterName(evaluation.getUserName());
					comment.setContent(evaluation.getPingjiaDetail());
					comment.setCreateTime(evaluation.getCreateTime()==null?"":evaluation.getCreateTime().toString());
					comment.setIsAnonymous(false);
					comment.setPoint(Integer.valueOf(evaluation.getPingfen()));
					comment.setSkuName(itemDetail.getItemZhName());
					commentList.add(comment);
				}
			}
			productDetail.setCommentList(commentList);
			productDetail.setCommentCount(commentList.size());
			productDetail.setCommentPoint(5F);
			productDetail.setDesc(itemDetail.getDescription());

			productDetail.setDetailUrl(itemDetail.getWxdetail());

			// ------------------主图
			List<String> imgList = new ArrayList<String>();
			List<ItemPicture> itemPictureList = itemDetail.getItemPictures();
			for (ItemPicture pic : itemPictureList) {
				imgList.add(pic.getPictureUrl());
			}
			productDetail.setImgList(imgList);

			productDetail.setName(itemDetail.getItemZhName());
			productDetail.setProductId(Long.parseLong(itemDetail.getItemId()
					.toString()));

			// ---------------------服务条款

			List<ImageDTO> serviceList = new ArrayList<ImageDTO>();
			ImageDTO img = new ImageDTO();
			img.setContent("content");
			img.setImgUrl("imgUrl");
			img.setType(1);
			serviceList.add(img);
			serviceList.add(img);
			productDetail.setServiceList(serviceList);

			// ---------------------sku

			List<SkuDTO> skuList = new ArrayList<SkuDTO>();
			List<PromotionDTO> promotionList = new ArrayList<PromotionDTO>();

			Map<ItemSku, List<SkuPropertyValueLink>> itemSkuDatas = itemDetail
					.getItemSkuDatas();
			Set<ItemSku> keySet = itemSkuDatas.keySet();
			int indexFirst = 0;
			for (ItemSku itemSku : keySet) {
				SkuDTO sku = new SkuDTO();
				sku.setImgUrl(itemSku.getImageUrl());
				sku.setName(itemSku.getSkuName());
				sku.setCurPrice(itemSku.getPrice());
				sku.setOriPrice(itemSku.getMarketPrice());
				sku.setStock(itemSku.getStock());
				sku.setSkuId(Long.valueOf(itemSku.getSkuId().toString()));
				skuList.add(sku);

				// ------------------促销信息

				List<ProRule> list = ruleService.listBelongToProRuleNotOutDate(
						itemSku.getSkuId(), 0);
				for (ProRule rule : list) {
					PromotionDTO promotion = new PromotionDTO();
					promotion.setDesc(rule.getRuleName());
					promotion.setPromotionId(Long.valueOf(rule.getRuleId()
							.toString()));
					if ("RULE_FULL_CUT".equals(rule.getType()))
						promotion.setType(0);
					if ("RULE_FULL_GIFT".equals(rule.getType()))
						promotion.setType(1);
					if ("RULE_DISCOUNT".equals(rule.getType()))
						promotion.setType(2);
					promotionList.add(promotion);
				}
			}

			productDetail.setSkuList(skuList);
			productDetail.setPromotionList(promotionList);

			response.setData(productDetail);
		} catch (Exception e) {
			logger.error("getProductDetail error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取商品详情错误");
			return response;
		}

		msg = "获取商品详情成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}
	
	/**
	 * 获取商品评价列表
	 * @param baseRequest
	 * @return
	 */
	@ServiceMethod(method = "item.getEvaluationList", version = "1.0", needInSession = NeedInSessionType.NO, httpAction = HttpAction.GET)
	public Response getEvaluationList(GetEvaluationRequest getEvaluationRequest) {
		Response response = new Response();
		String msg = "";

		Integer itemId = getEvaluationRequest.getItemId();
		Integer page = getEvaluationRequest.getPage();
		
		if(itemId==null||StringUtils.isBlank(itemId.toString())||page==null)
		{
			response.getResult().setResult("0");
			response.getResult().setMessage("参数不合法");
			return response;			
		}

		try {

			PageBean<EvaluationDto> pageBean = getItemEvaluation(
					itemId, page);
			List<EvaluationDto> evaluationList = pageBean.getResult();
			if (evaluationList == null) {
				response.getResult().setResult("0");
				response.getResult().setMessage("没有评价");
				return response;
			}

			response.setData(evaluationList);
		} catch (Exception e) {
			logger.error("getEvaluationList error", e);
			response.getResult().setResult("0");
			response.getResult().setMessage("获取商品评价列表错误");
			return response;
		}

		msg = "获取商品评价列表成功";
		response.getResult().setResult("1");
		response.getResult().setMessage(msg);
		return response;
	}
}
